package com.hardydou.system.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hardydou.common.result.Code;
import com.hardydou.common.result.Result;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.ResultAssert;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.log.anno.LogMetaInfo;
import com.hardydou.system.model.SystemTask;
import com.hardydou.system.service.ISystemTaskService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：系统任务控制器
 * @date ：Created in 2020/6/25
 * @Copyright ：Hardydou.com
 */
@Controller
@Api(tags = "系统任务")
@LogMetaInfo(module = "系统任务")
@RequestMapping("/system/task")
public class SysTaskController extends BaseController {

    @Autowired
    private ISystemTaskService taskService;


    @GetMapping("")
    public String index() {
        return "task/list";
    }

    @ApiIgnore
    @GetMapping("/add")
    public String add(Model model) {
        return "task/add";
    }

    @ApiIgnore
    @PostMapping("/add")
    public String add(SystemTask task, Model model) {
        taskService.addTask(task);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return "task/add";
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long taskId, Model model) {
        SystemTask task = taskService.findById(taskId);
        model.addAttribute("task", task);
        return "task/edit";
    }

    @ApiIgnore
    @PutMapping("/edit")
    public String edit(SystemTask task, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        taskService.update(task);
        model.addAttribute("task", task);
        return "task/edit";
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(long taskId, Model model) {
        SystemTask task = taskService.findById(taskId);
        model.addAttribute("task", task);
        return "task/info";
    }


    /* JSON API */

    @ApiOperation(value = "任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listForJson(int pageNum, int pageSize, @RequestParam Map params) {
        PageHelper.startPage(getRequest());
        List<SystemTask> tasks = taskService.list(params);
        PageInfo<SystemTask> pageInfo = new PageInfo<>(tasks);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "添加任务")
    @PostMapping("/add.json")
    @ResponseBody
    public Result<?> addForJson(SystemTask task) {
        taskService.addTask(task);
        return ResultHelper.ok(task);
    }


    @ApiOperation(value = "删除任务")
    @ApiImplicitParam(name = "taskIds", value = "任务ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result delete(String taskIds) {
        long[] ids = StrUtil.splitToLong(taskIds, ",");
        for (long id : ids) {
            taskService.delTask(id);
        }
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("taskIds", ids);
        return result;
    }

    @ApiOperation(value = "更新任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", required = true, paramType = "query")
    })
    @ApiParam(name = "taskId", value = "任务ID", required = true)
    @PutMapping("/update")
    @ResponseBody
    public Result update(SystemTask task) {
        taskService.update(task);
        return ResultHelper.ok(task);
    }

    @ApiOperation(value = "更新任务状态")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "taskId", value = "任务ID", required = true)
    )
    @PutMapping("/status")
    @ResponseBody
    public Result status(long taskId) {
        SystemTask task = taskService.findById(taskId);
        ResultAssert.notNull(task, Code.TASK_NO_EXIST);
        if (task.isOn()) {
            taskService.stopTask(task);
        } else {
            taskService.startTask(task);
        }
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("taskId", taskId);
        result.getData().put("status", task.getStatus());
        return result;
    }
}
